package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.UserChildDtoMapper;
import com.starlink.model.domain.UserChildDO;
import com.starlink.mapper.UserChildMapper;
import com.starlink.model.request.UserChildRequest;
import com.starlink.service.UserChildService;
import com.starlink.model.util.UserChildConvert;
import com.starlink.model.vo.UserChildVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class UserChildServiceImpl extends ServiceImpl<UserChildMapper, UserChildDO> implements UserChildService {
    @Autowired
    private UserChildMapper userChildMapper;

    @Override
    public UserChildVO getByKey(long id) {
        UserChildVO userChildVO = UserChildDtoMapper.INSTANCE.do2VO(getById(id));
        return userChildVO;
    }

    @Override
    public PageInfo<UserChildVO> pageQuery(Pageable pageable,UserChildRequest request) {
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<UserChildDO> list = userChildMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<UserChildVO> userChildVOList = UserChildConvert.buildUserChildVoList(list);
        return new PageInfo(userChildVOList);
    }

    @Override
    public List<UserChildVO> listQuery(UserChildRequest request) {
        List<UserChildDO> list = userChildMapper.selectDOList(UserChildDtoMapper.INSTANCE.request2DO(request));
        List<UserChildVO> userChildVOS = UserChildConvert.buildUserChildVoList(list);
        return userChildVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(UserChildRequest request) {
        save(UserChildDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(UserChildRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(UserChildDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        userChildMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
